package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import dao.GmtypeDao;
import dbpool.DbPool;
import entity.Gmtype;

public class GmtypeDaoImpl implements GmtypeDao{
	
	QueryRunner runner = new QueryRunner();
	
	@Override
	public Gmtype gettypename(int type_id) throws SQLException {
		DbPool dbpool = DbPool.getPool();
		Connection conn = dbpool.getConn();
		String sql = "select * from gmtype where type_id=?";
		
		Gmtype gmtype = (Gmtype) runner.query(conn,sql,new BeanHandler(Gmtype.class),type_id);
		System.out.println(gmtype);
		dbpool.back(conn);
		return gmtype;
	}

	@Override
	public int adddata(String type_name) throws SQLException {
		DbPool dbpool = DbPool.getPool();
		Connection conn = dbpool.getConn();
		String sql = "insert into gmtype values(null, ?)" ;
		QueryRunner runner = new QueryRunner();
		int n = runner.update(conn, sql, type_name);
		System.out.println("增加成功");
		dbpool.back(conn);
		return n;
	}
	
	@Override
	public int updata(String type_name, int type_id ) throws SQLException {
		DbPool dbpool = DbPool.getPool();
		Connection conn = dbpool.getConn();
		String sql="update gmtype set type_name=? where type_id=?";
		
		int   m =   runner.update(conn,sql,type_name,type_id); 
	    System.out.println("修改成功");
	    dbpool.back(conn);
		return m;
	}

	@Override
	public int deldata( int type_id) throws SQLException {
		DbPool dbpool = DbPool.getPool();
		Connection conn = dbpool.getConn();
		String sql="delete from gmtype where type_id=?";
		int   l =   runner.update(conn,sql,type_id); 
		return l;
	}

}